// ==================================================================================
// Shared Genomics Project MPI Codebase
// Version 1.0 30/04/2010
//
// (c) 2010 University of Manchester all rights reserved
//
// This file is distributed under the GNU General Public License, Version 2.  
// Please see the file COPYING.txt for more details
// ==================================================================================

/*! 
\file
\brief Main body of CORRECT_MATRIX
*/
/*! 
\defgroup	correct_matrix CORRECT_MATRIX - Formatting for matrix import into R.
\ingroup    mpi_stats
\brief		CORRECT_MATRIX - Formatting for matrix import into R.
\details
<p>
This program corrects data output errors from the PLINK LD calculation.
As LD is a quick calculation, the decision was made to use with the LD implementation of PLINK v.107.
PLINK generates a square matrix associated with an LD calculation centered on a particular SNP.
This matrix file can then imported into R to generate a heatmap graphic.
LD calculation may involve a divide by zero, resulting in a string with the format <em>INF</em> in
the matrix file generated by PLINK.The inclusion of an <em>INF</em> in the matrix file causes data import into R to fail.
</p>
<p>This program simply tokenises the matrix file generated by PLINK and replaces <em>INF</em> strings with
the R <em>NA</em> symbol.
</p>
<p>Program takes a single command-line argument which is the path to a text matrix file.
The output file path is the same as the input file path but the filename is appended with suffix '.2'.
The entire matrix file is placed into a string array prior to parsing and this program 
uses a maximum filepath length of \ref FILE_PATH_LENGTH.
</P>

<p>The program exits with -1 on error.</p>

<h3>Usage</h3>
<pre>correct_matrix.exe /somepath/dataset.ld</pre>
<p>The resulting output file would be <em>/somepath/dataset.ld.2</em> .</p>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "options.h"
#include "utils.h"

/*!
\brief Main body of the CORRECT_MATRIX program
\ingroup correct_matrix
*/
int main(int argc, char *argv[])
{
	BOOL jobDone = FALSE;
	char matrixPath[FILE_PATH_LENGTH], newMatrixPath[FILE_PATH_LENGTH], **lines = NULL, token[TOKEN_LENGTH];
	int nLines = 0, nTokens = 0, i;
	register int j;

	if (argc == 1) return -1;

	strcpy(matrixPath, argv[1]);
	sprintf(newMatrixPath, "%s.2", matrixPath);

	lines = load_file(matrixPath, &nLines);
	if (lines != NULL && nLines > 0) {
		nTokens = get_number_of_tokens(lines[0], ' ');
		if (nTokens >= 1) {
			FILE *out = fopen(newMatrixPath, "w");
			if (out != NULL) {
				for (i = 0; i < nLines; i++) {
					for (j = 0; j < nTokens; j++) {
						get_token(lines[i], token, j, ' ');
						if (strchr(token, '#') != NULL) fprintf(out, "NA "); // Infinity string have a hash character under Win32 compiler.
						else fprintf(out, "%s ", token);
					}
					fprintf(out, "\n");
				}
				fclose(out);
				jobDone = TRUE;
			}
		}
	}
	if (lines != NULL) free_2d_char_array(lines, nLines);

	if (jobDone) return 0;
	else return -1;
}